package cn.minhx.domain.activity.service.discount.impl;

import cn.minhx.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import cn.minhx.domain.activity.service.discount.AbstractDiscountCalculator;
import com.alibaba.fastjson2.JSON;
import java.math.BigDecimal;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 满减类型计算
 *
 * @author min
 */
@Slf4j
@Service("MJ")
public class MJCalculator extends AbstractDiscountCalculator {

	@Override
	protected BigDecimal doCalculate(BigDecimal originalPrice,
			GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount) {
		log.info("优惠策略-满减计算 originalPrice:{} groupBuyDiscount:{}", originalPrice, JSON.toJSONString(groupBuyDiscount));
		// 折扣表达式 - 100,10 满100减10元
		String marketExpr = groupBuyDiscount.getMarketExpr();
		String[] split = marketExpr.split(SPLITTER);
		BigDecimal threshold = new BigDecimal(split[0].trim());
		BigDecimal deductionAmount = new BigDecimal(split[1].trim());
		// 不满足最低满减约束，则按照原价
		if (originalPrice.compareTo(threshold) < 0) {
			return originalPrice;
		}
		// 折后价格
		return originalPrice.subtract(deductionAmount);
	}
}
